package k4_thread.demo02;

import java.util.Random;

public class DownloadFile extends Thread{

    Random random = new Random();
    private String fileName;

    public DownloadFile(String fileName,String threadName) {
        super(threadName);
        this.fileName = fileName;
    }

    /**
     * 此类没有 Runnable 所以必须重写 run 方法
     * 把线程中需要执行的逻辑写入此方法里
     */
    @Override
    public void run() {
        try {
            for (int i = 1; i <=100 ; i++) {
                System.out.println(getName() +" 下载了 "+i+"%");
                Thread.sleep(random.nextInt(500)+100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

class Test{

    public static void main(String[] args) {

        System.out.println("开始下载了。。。。。。");

        Thread thread1 = new DownloadFile("java从入门到精通","java从入门到精通");
        Thread thread2 = new DownloadFile("颈椎病的处理","颈椎病的处理");
        Thread thread3 = new DownloadFile("高血压的预防","高血压的预防");
        Thread thread4 = new DownloadFile("腰间盘突出的治疗","腰间盘突出的治疗");
        Thread thread5 = new DownloadFile("活着","活着");

        thread1.setName("java从精通到入魔");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();


        try {
            //先运行当前线程，当前线程运行完毕后再运行主线程
            thread1.join();
            thread2.join();
            thread3.join();
            thread4.join();
            thread5.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("下载结束了");

    }


}



